var express = require('express'),
	superagent = require('superagent'),
	cheerio = require('cheerio'),
	async = require('async');

	var app = new express(),
	targetUrl = 'https://cnodejs.org/';

	app.get('/',function(req,res,next){
        async.parallel({
            one:function(callback){
                superagent.get(targetUrl)
                    .end(function(err,data){
                        if(err) return next(err);

                        var $ = cheerio.load(data.text);
                        var array1 = [];

                        $('#topic_list .topic_title').each(function(index,element){
                            var $element = $(element);
                            array1.push({
                                title:$element.attr('title')
                            });
                        });
                        callback(null,array1);
                    });
            },
            two:function(callback){
                superagent.get(targetUrl)
                    .end(function(err,data){
                        if(err) return next(err);

                        var $ = cheerio.load(data.text);
                        var array2 = [];

                        $('#topic_list .topic_title').each(function(index,element){
                            var $element = $(element);
                            array2.push({
                                href:$element.attr('href')
                            });
                        });
                        callback(null,array2);
                    });
            }
        },function(err,result){
            if(err){
                console.log(err);
            }
            var obj = {
                array1:result.array1,
                array2:result.array2
            }
            res.send(obj);
        });
	});

	app.listen(3000,function(){
	    console.log('server start...');
	});